package example.springboot.controller;

import example.springboot.common.Result;
import example.springboot.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @Author 董熠涛
 * @Date 2025/6/16 20:22
 */
@RestController
@RequestMapping("/api/stats")
@CrossOrigin
public class StatsApiController {

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 查看学习进度
     */
    @GetMapping("/progress")
    public Result<Map<String, Object>> getProgress(HttpServletRequest request) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

         
            
            Map<String, Object> progress = new HashMap<>();
            progress.put("learnedWords", 0);
            progress.put("unlearnedWords", 0);
            progress.put("totalWords", 0);
            
            return Result.success(progress);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取学习进度失败：" + e.getMessage());
        }
    }

    /**
     * 查看每日学习量
     */
    @GetMapping("/daily")
    public Result<List<Map<String, Object>>> getDailyStats(HttpServletRequest request) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

           
            
            return Result.success("获取成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取每日学习量失败：" + e.getMessage());
        }
    }

    /**
     * 词汇掌握分布
     */
    @GetMapping("/words")
    public Result<Map<String, Integer>> getWordMastery(HttpServletRequest request) {
        try {
            Integer userId = getUserIdFromRequest(request);
            if (userId == null) {
                return Result.error("未登录");
            }

         
            
            Map<String, Integer> mastery = new HashMap<>();
            mastery.put("mastered", 0);    // 熟练
            mastery.put("familiar", 0);    // 模糊
            mastery.put("forgotten", 0);   // 遗忘
            
            return Result.success(mastery);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取词汇掌握分布失败：" + e.getMessage());
        }
    }

    /**
     * 从请求中获取用户ID
     */
    private Integer getUserIdFromRequest(HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            
            if (token == null || token.isEmpty()) {
                return null;
            }

            return jwtUtil.getUserIdFromToken(token);
        } catch (Exception e) {
            return null;
        }
    }
}

